﻿using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace HuiZe.education.View
{
    /// <summary>
    /// MyShareResource.xaml 的交互逻辑
    /// </summary>
    public partial class MyShareResource : UserControl
    {
        public MainViewModel mainView { get; set; }

        public MyShareResource()
        {
            InitializeComponent();
            Loaded += AccountLoginView_Loaded;
            pageManage.LoadDataCallback = updata;
            PART_Search.PreviewMouseLeftButtonUp += PART_Search_PreviewMouseLeftButtonUp;
        }

        void PART_Search_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource != null && e.OriginalSource is Button)
            {
                updata();
            }
        }

        private void AccountLoginView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            MainViewModel mainViewModel = DataContext as MainViewModel;
            mainView = mainViewModel;

            updata();
            //updata(0, 1);
            //pageManage.ItemSourceCount = mainView.PageCount;
        }

        void updata()
        {
            updata(1);
        }
        void updata(int pages)
        {
            updata(0, pages, PART_Search.Text);
        }
        private void updata(int type, int pages, string _key = "")
        {
            //ImageList.Children.Clear();
            //var list = mainView.GetMyShareList(type, pages);
            //foreach (var item in list)
            //{
            //    mybkBookEdit5 book = new mybkBookEdit5();
            //    book.NomalImage = item.image_cover;
            //    book.bookName = item.title;
            //    book.shareName = item.nickname;
            //    book.createTime = item.createtime_text.ToString();
            //    book.Id = item.id.ToString();
            //    book.Margin = new Thickness(10, 10, 0, 0);
            //    book.Height = 250;
            //    book.Width = 200;
            //    book.DownLoadUrl = item.zippath;
            //    book.DelImg.MouseLeftButtonDown += DelImg_MouseDown;

            //    ImageList.Children.Add(book);
            //}

            _list_res = mainView.GetMyShareList(type, pages, _key);
            PART_MyShareItems.ItemsSource = _list_res;
            pageManage.Visibility = (_list_res != null && _list_res.Count > 0) ? Visibility.Visible : Visibility.Collapsed;
            if (pageManage.Visibility == Visibility.Visible)
            {
                pageManage.ItemSourceCount = mainView.PageCount;
                if (_page_count_last != mainView.PageCount)
                    pageManage.UpdataPages();

                _page_count_last = mainView.PageCount;
            }
        }

        private void DelImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            AttendClassViewModel attendClass = DataContext as AttendClassViewModel;
            var button = sender;
            FrameworkElement __do = sender as FrameworkElement;
            mybkBookEdit bookbtn = new mybkBookEdit();
            mybkBookEdit5 bookbtn2 = new mybkBookEdit5();
            while (__do != null)
            {
                __do = __do.Parent as FrameworkElement;
                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn = __do.Parent as mybkBookEdit;
                    //ParentView.Update0();
                    break;
                }
                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit5")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn2 = __do.Parent as mybkBookEdit5;
                    //ParentView.Update0();
                    break;
                }

                if (__do.Parent == null) break;
            }

            if (!string.IsNullOrWhiteSpace(bookbtn.Id))
            {
                mainView.DelBclass(Convert.ToInt32(bookbtn.Id));
                updata(0, 1);
            }

            if (!string.IsNullOrWhiteSpace(bookbtn2.Id))
            {
                mainView.DelBclass(Convert.ToInt32(bookbtn2.Id));
                updata(0, 1);
            }
        }

        void PART_MyShare_Item_Loaded(object sender, RoutedEventArgs e)
        {
            PART_MyShare_Item2 _item = (PART_MyShare_Item2)sender;
            _item.DeleteCallback = MyShare_Delete;
            _item.Load_Image();
        }

        void MyShare_Delete(resShareList _data)
        {
            if (_data == null) return;

            ShowDialogView3 showDialogView = new ShowDialogView3();
            showDialogView.ShowDialogContent = "确定要删除 ?";
            if (showDialogView.ShowDialog() == true)
            {
                mainView.DelBclass(_data.id);
            }
        }

        private void PageManage_PreviewMouseUp_1(object sender, MouseButtonEventArgs e)
        {
            //if (e.OriginalSource.ToString().Contains("CustomRadioButton"))
            //{
            //    CustomRadioButton button = e.OriginalSource as CustomRadioButton;
            //    string index = button.Content.ToString();
            //    if (index == ">")
            //    {
            //        //index = pageManage.Content.ToString();
            //        index = pageManage.CurrentIndex < pageManage.ItemSourceCount ? (pageManage.CurrentIndex + 1).ToString() : pageManage.CurrentIndex.ToString();
            //    }
            //    if (index == "<")
            //    {
            //        index = pageManage.CurrentIndex > 1 ? (pageManage.CurrentIndex - 1).ToString() : "1";
            //    }
            //    if (index == "首页")
            //    {
            //        index = "1";
            //    }
            //    if (index == "尾页")
            //    {
            //        index = pageManage.ItemSourceCount.ToString();
            //    }
            //    //updata(0, Convert.ToInt32(index));
            //    updata(Convert.ToInt32(index));
            //}
        }

        int _page_count_last;
        List<resShareList> _list_res;
    }
}
